package ncorps.commun;

import java.util.ArrayList;
import org.apache.commons.math.stat.StatUtils;

public class StatNewtonFourier {

    private double differenceMoyenne_X;
    private double differenceMoyenne_Y;
    private double variance_X;
    private double variance_Y;
    private ArrayList<ConditionInitiale> LCIf,  LCIn;
    private int _N;
    private int tempsMax;
    private double[][][] bodytraj_F,  bodytraj_N;
    private double[][] deltaposition;

    public StatNewtonFourier(CalculThread th) {
        LCIf = th.LCIf;
        LCIn = th.LCIn;
        _N = th._N;
        tempsMax = th.tempsMax;
        bodytraj_F = new double[_N][5][tempsMax];
        bodytraj_N = new double[_N][5][tempsMax];
        for (int n = 0; n < _N; n++) {
            for (int t = 0; t < tempsMax; t++) {
                for (int k = 0; k < 5; k++) {
                    bodytraj_F[n][k][t] = LCIf.get(t).getGcoord()[n][k];
                    bodytraj_N[n][k][t] = LCIn.get(t).getGcoord()[n][k];
                }
            }
        }
        int p = 50;
        deltaposition = deltaPos(bodytraj_F, bodytraj_N);

    }

    private double[][] deltaPos(double[][][] bodytraj_F, double[][][] bodytraj_N) {
        double[][] delta = new double[_N][tempsMax];
        double xf, yf, xn, yn;
        for (int p = 0; p < _N; p++) {
            for (int t = 0; t < tempsMax; t++) {
                xf = bodytraj_F[p][1][t];
                yf = bodytraj_F[p][2][t];
                xn = bodytraj_N[p][1][t];
                yn = bodytraj_N[p][2][t];
                delta[p][t] = norm(xn, yn, xf, yf);
            }
        }
        return delta;
    }

    private double norm(double xn, double yn, double xf, double yf) {
        return Math.sqrt((xn - xf) * (xn - xf) + (yn - yf) * yn - yf);
    }

    StatNewtonFourier(ArrayList<ConditionInitiale> LCIf, ArrayList<ConditionInitiale> LCIn, int t) {
        int Nb = LCIf.get(0).get_N();
        double[][] gcoordF = LCIf.get(t).getGcoord();
        double[][] gcoordN = LCIn.get(t).getGcoord();
        double[] gcoordF_1 = new double[Nb], gcoordF_2 = new double[Nb], gcoordN_1 = new double[Nb], gcoordN_2 = new double[Nb];

        for (int n = 0; n < Nb; n++) {
            gcoordF_1[n] = gcoordF[n][1];
            gcoordF_2[n] = gcoordF[n][2];
            gcoordN_1[n] = gcoordN[n][1];
            gcoordN_2[n] = gcoordN[n][2];
        }
        differenceMoyenne_X = StatUtils.meanDifference(gcoordF_1, gcoordN_1);
        differenceMoyenne_Y = StatUtils.meanDifference(gcoordF_2, gcoordN_2);

        variance_X = StatUtils.varianceDifference(gcoordF_1, gcoordN_1, differenceMoyenne_X);
        variance_Y = StatUtils.varianceDifference(gcoordF_2, gcoordN_2, differenceMoyenne_X);
    }

    public double[][][] getBodytraj_F() {
        return bodytraj_F;
    }

    public double[][][] getBodytraj_N() {
        return bodytraj_N;
    }

    public int get_N() {
        return _N;
    }

    public int getTempsMax() {
        return tempsMax;
    }

    public double[][] getDeltaposition() {
        return deltaposition;
    }

    public double getDifferenceMoyenne_X() {
        return differenceMoyenne_X;
    }

    public double getDifferenceMoyenne_Y() {
        return differenceMoyenne_Y;
    }

    public double getVariance_X() {
        return variance_X;
    }

    public double getVariance_Y() {
        return variance_Y;
    }

}
